package com.itheima.service.message.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheiam.service.message.ProductService;
import com.itheima.dao.message.ProductDao;
import com.itheima.domain.message.Product;
import com.itheima.domain.message.ProductExample;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDao productDao;

    @Override
    public void save(Product product) {
        product.setId(UUID.randomUUID().toString());
        product.setUpdateTime(new Date());
        product.setCreateTime(new Date());
        productDao.insertSelective(product);
    }

    @Override
    public void update(Product product) {
        product.setUpdateTime(new Date());
        productDao.updateByPrimaryKeySelective(product);
    }

    @Override
    public void delete(String id) {
        productDao.deleteByPrimaryKey(id);
    }

    @Override
    public Product findById(String id) {
        return productDao.selectByPrimaryKey(id);
    }

    @Override
    public List<Product> findAll(ProductExample example) {
        return productDao.selectByExample(example);
    }

    @Override
    public PageInfo<Product> findAll(ProductExample example,Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Product> list = productDao.selectByExample(example);
        return new PageInfo<>(list);
    }
}
